package com.cy.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;

import com.cy.util.ConnectionFactory;

public class ChangeDaoImpl implements ChangeDao {

	@Override
	public int resetPwd(int id) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int flag = 0;
		try {
			String sql = "update user set password=? where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, "123456");
			psmt.setInt(2, id);
			flag = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return flag;
	}

	@Override
	public int delete(int id, int state) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int flag = 0;
		try {
			String sql = "update user set state=? where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, state);
			psmt.setInt(2, id);
			flag = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return flag;
	}

	@Override
	public int change(int id, int state) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int flag = 0;
		try {
			String sql = "update games set state=? where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, state);
			psmt.setInt(2, id);
			flag = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return flag;
	}

	@Override
	public int buy(int userid, int gameid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int flag = 0;
		try {
			String sql = "insert into sell_info (user_id,game_id,sell_date) values (?,?,?)";
			psmt = conn.prepareStatement(sql);
			psmt.setInt(1, userid);
			psmt.setInt(2, gameid);
			SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");// 设置日期格式
			String adddate = df.format(new Date());
			psmt.setString(3, adddate);
			flag = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return flag;
	}

	@Override
	public int changeusr(String username, int userid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int flag = 0;
		try {
			String sql = "update user set username=? where (select id from (select id from user where username =? ) t1) is null and id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, username);
			psmt.setString(2, username);
			psmt.setInt(3, userid);
			flag = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return flag;
	}

	@Override
	public int changepwd(String newpwd, int userid) {
		Connection conn = ConnectionFactory.getConnection();
		PreparedStatement psmt = null;
		int flag = 0;
		try {
			String sql = "update user set password=? where id=?";
			psmt = conn.prepareStatement(sql);
			psmt.setString(1, newpwd);
			psmt.setInt(2, userid);
			flag = psmt.executeUpdate();
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(conn, psmt, null);
		}
		return flag;
	}

}
